package com.estronger.shopmanage.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.AbnormalType;
import com.estronger.shopmanage.ui.order.activity.SelectActionActivity;
import com.estronger.shopmanage.ui.order.bean.OrderAttributeBean;

import java.util.ArrayList;
import java.util.List;

/**
 * author : DC-DingYG
 * e-mail : dingyg012655@126.com
 * time : 2021/04/26
 * desc : * 异常申报类型共用view
 */
public class AbnormalDeclareView extends LinearLayout implements WiperSwitch.OnChangedListener{

    private Activity mActivity;
    private CallBack callBack;
    private TextView tvIfCreateAbn;//栏位描述，如 是否产生送货异常申报等
    private WiperSwitch wSwitch;
    private RelativeLayout rlfAbnormalType;
    private LinearLayout llfAbnormal;
    private LinearLayout llChoiseAbType;//选择异常申报类型
    private TextView tvfAbnormalType;
    private TextView tvfAbnormalT;
    private EditText et_abnormal;
    private ArrayList<OrderAttributeBean> typeList;//开窗的类型列表
    public ResultBean resultBean;
    private int viewType;//类型 0-安装完工 1-送货完工

    public static final int REQUEST_CODE_CHOISE_AB_TYPE = 83;//选中异常类型后
    public static final String BIND_DATA = "SELECT_ABNOMALDECL";//传的绑定的数据

    public AbnormalDeclareView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.include_abnormal_declaration, this);
        tvIfCreateAbn = view.findViewById(R.id.tvIfCreateAbn);
        wSwitch = view.findViewById(R.id.wsfAbnDesc);
        rlfAbnormalType = view.findViewById(R.id.rlfAbnormalType);
        llfAbnormal = view.findViewById(R.id.llfAbnormal);
        llChoiseAbType = view.findViewById(R.id.ll_check_abnormal);
        tvfAbnormalType = view.findViewById(R.id.tvfAbnormalType);
        tvfAbnormalT = view.findViewById(R.id.tvfAbnormalT);
        et_abnormal = view.findViewById(R.id.et_abnormal);
        resultBean = new ResultBean();
    }

    /**
     * 初始化
     * @param mActivity
     * @param fDesc 描述
     * @param fcallBack 回调
     * @param viewType 类型 0-送货 1-安装
     */
    public void initAView(Activity mActivity, String fDesc, CallBack fcallBack, int viewType) {
        this.mActivity = mActivity;
        tvIfCreateAbn.setText(fDesc == null ? "" : fDesc);
        this.callBack = fcallBack;
        this.viewType = viewType;
        if(viewType == 1) {
            tvfAbnormalT.setText("投诉诉求");
            et_abnormal.setHint("请描述投诉内容，或填写需换货部件/需补货零件/需维修部件等");
        }else {
            tvfAbnormalT.setText("异常描述");
            et_abnormal.setHint("请描述异常");
        }
        wSwitch.setOnChangedListener(this);
        llChoiseAbType.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (typeList == null || typeList.size() == 0) {
                    if(callBack != null) callBack.getAbnormalType();
                }else {
                    showListDialog(0, null);
                }
            }
        });
    }

    /**
     * 展示列表Dialog
     * @param type 0是不需要赋值
     * @param types
     */
    public void showListDialog(int type, List<AbnormalType.TypeBean> types) {
        if(type == 1) {
            typeList = new ArrayList<>();
            for (AbnormalType.TypeBean typeBean : types){
                OrderAttributeBean bean = new OrderAttributeBean();
                bean.setAttId(typeBean.getFcode());
                bean.setAttName(typeBean.getFdesc());
                typeList.add(bean);
            }
        }

        SelectActionActivity.openActivity(mActivity, 7, typeList, resultBean.getTypeCode(), REQUEST_CODE_CHOISE_AB_TYPE);
    }

    @Override
    public void OnChanged(WiperSwitch wiperSwitch, boolean checkState) {
        if (checkState) {
            if(viewType == 1) {
                rlfAbnormalType.setVisibility(GONE);
            }else {
                rlfAbnormalType.setVisibility(GONE);
            }
            llfAbnormal.setVisibility(View.VISIBLE);
            resultBean.setIfGenAdOrder(1);
        } else {
            rlfAbnormalType.setVisibility(View.GONE);
            llfAbnormal.setVisibility(View.GONE);
            resultBean.setIfGenAdOrder(0);
        }
    }
    //选中类型后界面设值
    public void setAbTypeText(String typeCode, String typeDesc) {
        resultBean.setTypeCode(typeCode);
        tvfAbnormalType.setText(typeDesc == null ? "" : typeDesc);
    }

    //提交时获取数据
    public ResultBean getDatas() {
        resultBean.setAbnormalDesc(et_abnormal.getText().toString().trim());
        return resultBean;
    }

    public interface CallBack {
        void getAbnormalType();
    }

    public class ResultBean {
        private int ifGenAdOrder;//是否产生异常申报单
        private String typeCode;//选中的异常类型代号
        private String typeDesc;//选中的异常类型描述
        private String abnormalDesc;//异常描述

        public int getIfGenAdOrder() {
            return ifGenAdOrder;
        }

        public void setIfGenAdOrder(int ifGenAdOrder) {
            this.ifGenAdOrder = ifGenAdOrder;
        }

        public String getTypeCode() {
            return typeCode == null ? "" : typeCode;
        }

        public void setTypeCode(String typeCode) {
            this.typeCode = typeCode;
        }

        public String getTypeDesc() {
            return typeDesc;
        }

        public void setTypeDesc(String typeDesc) {
            this.typeDesc = typeDesc;
        }

        public String getAbnormalDesc() {
            return abnormalDesc == null ? "" : abnormalDesc;
        }

        public void setAbnormalDesc(String abnormalDesc) {
            this.abnormalDesc = abnormalDesc;
        }
    }
}
